package com.sharker.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.sharker.R;

/**
 * 课程首页适配
 */

public class CourseAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener {
    private Context context;
    private OnCourseClickListener mOnCourseClickListener;
    private RecyclerView recyclerView;

    public void setmOnCourseClickListener(OnCourseClickListener mOnCourseClickListener) {
        this.mOnCourseClickListener = mOnCourseClickListener;
    }

    public CourseAdapter(Context context) {
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_course, parent, false);
        return new ItemViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
        itemViewHolder.titleTv.setText("连锁门店课程大纲");
        itemViewHolder.title_cpTv.setText("知名美业顾问免费");
        itemViewHolder.nameTv.setText("于春华老师");
        itemViewHolder.timeTv.setText("1小时30分");
        //进行判断
        itemViewHolder.testTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnCourseClickListener.onCourseTestClick(position);
            }
        });
        itemViewHolder.itemView.setOnClickListener(this);
    }

    @Override
    public int getItemCount() {
        return 10;
    }

    //绑定
    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        this.recyclerView = recyclerView;
    }

    //解绑
    @Override
    public void onDetachedFromRecyclerView(RecyclerView recyclerView) {
        super.onDetachedFromRecyclerView(recyclerView);
        this.recyclerView = null;
    }

    @Override
    public void onClick(View v) {
        if (recyclerView != null && mOnCourseClickListener != null) {
            int position = recyclerView.getChildAdapterPosition(v);
            mOnCourseClickListener.onCourseDesClick(position);
        }
    }

    static class ItemViewHolder extends RecyclerView.ViewHolder {
        TextView titleTv, title_cpTv, nameTv, timeTv,testTv;
        ImageView imageView;

        ItemViewHolder(View itemView) {
            super(itemView);
            imageView = (ImageView) itemView.findViewById(R.id.course_titleimage);
            titleTv = (TextView) itemView.findViewById(R.id.course_title);
            title_cpTv = (TextView) itemView.findViewById(R.id.courseitem_classname);
            nameTv = (TextView) itemView.findViewById(R.id.courseitem_name);
            timeTv = (TextView) itemView.findViewById(R.id.courseitem_time);
            testTv = (TextView) itemView.findViewById(R.id.courseitem_orangetext);
        }
    }

    /**
     * 定义接口
     */
    //定义一个接口
    public interface OnCourseClickListener {
        //详情
        void onCourseDesClick(int position);

        //测试
        void onCourseTestClick(int position);
    }
}
